package com.xgb.springcloudserver.service;

import com.xgb.springcloudserver.config.RibbonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

/**
 * @description:
 * @author: chenzuolin
 * @time: 2021/3/8 22:12
 */
@Service
public class RibbonService {

    private static final String SERVICE_URL = "http://eureka-client-provider/";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    public String hi(String name) {
        return restTemplate.getForObject(SERVICE_URL + "provider" , String.class);
    }

    public void hello(){
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            System.out.println(service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("eureka-client-provider");
        for (ServiceInstance s : instances) {
            System.out.println(s);
        }
        String host = instances.get(0).getHost();
        int port = instances.get(0).getPort();
        URI uri = instances.get(0).getUri();
        System.out.println(uri);
    }
}
